package view;

import view.backend.user.AdminUser;
import view.backend.graveyard.AdminGraveYard;
import view.backend.product.AdminProduct;
import view.backend.customer.AdminCustomer;
import view.generaldesign.UserLoggedInPanel;
import view.frontend.Login;
import view.generaldesign.*;
import java.util.ArrayList;
import javax.swing.*;
import view.frontend.StorageList;
import view.backend.*;
import view.backend.backup.AdminBackup;
import view.frontend.order.OrderContainer;
import view.menu.Menu;

/**
 * Denne klasse håndterer alle views, som registreres på JFramen
 * @author Sonny Sandberg
 */
public abstract class ViewHandler
{
    /**
     * Opret alle views og registrér dem i Singleton for View
     */
    public static void initAllViews()
    {
        Menu menuTop = new Menu();
        Menu menuLeft = new Menu();
        
        view.ViewSingleton.getSingletonObject().getMenus()[0] = menuTop;
        view.ViewSingleton.getSingletonObject().getMenus()[1] = menuLeft;
        
        ArrayList<ViewContainer> allViews = view.ViewSingleton.getSingletonObject().getAllViews();
        allViews.add(new ViewContainer(menuTop.getPanel(), "menuTop", 0, 0, null));
        allViews.add(new ViewContainer(menuLeft.getPanel(), "menuLeft", 0, 0, null));
        allViews.add(new ViewContainer(new PageTitle().getPanel(), "pageTitle", 300, 300, null));
        allViews.add(new ViewContainer(new Login().getPanel(), "login", 400, 500, "Login"));
        allViews.add(new ViewContainer(new UserLoggedInPanel().getPanel(), "userLoggedInPanel", 0, 0, null));
        allViews.add(new ViewContainer(new AdminUser().getPanel(), "adminUser", 0, 0, "Administrér Bruger"));        
        allViews.add(new ViewContainer(new AdminProgramValues().getPanel(), "adminProgramValues", 0, 0, "Moms / Miljøafgift"));
        
        AdminCustomer adminCustomer = new AdminCustomer();
        ViewSingleton.getSingletonObject().setAdminCustomer(adminCustomer);
        allViews.add(new ViewContainer(adminCustomer.getPanel(), "adminCustomer", 0, 0, "Administrér Kunde")); 
        
        AdminGraveYard adminGraveYard = new AdminGraveYard();
        ViewSingleton.getSingletonObject().setAdminGraveYard(adminGraveYard);
        allViews.add(new ViewContainer(adminGraveYard.getPanel(), "adminGraveYard", 0, 0, "Administrér Kirkegård"));
        
        allViews.add(new ViewContainer(new OrderContainer().getPanel(), "orderContainer", 0, 0, "Ordre"));
        allViews.add(new ViewContainer(new AdminProduct().getPanel(), "adminProduct", 0, 0, "Administrér Vare"));
        allViews.add(new ViewContainer(new AdminProductFamily().getPanel(), "adminProductFamily", 0, 0, "Administrér Varegruppe"));
        allViews.add(new ViewContainer(new StorageList().getPanel(), "storageListPanel", 0, 0, "Lagerstatus"));        
        allViews.add(new ViewContainer(new AdminBackup().getPanel(), "backUp", 0, 0, "Backup Administration"));
        allViews.add(new ViewContainer(new DesignLayout().getPanel(), "designLayout", 0, 0, null));
    }
    
    /**
     * Tilføj samtlige registrerede views til JFrame
     */
    public static void addAllViewsToMainFrame()
    {
        ArrayList<ViewContainer> mostViews = view.ViewSingleton.getSingletonObject().getAllViews();
        JFrame mainFrame = view.ViewSingleton.getSingletonObject().getMainFrame();
        
        for(ViewContainer value : mostViews)
        {
            mainFrame.add(value.getTheView());
        }
    }
    
    /**
     * Slå view til eller fra (visibility)
     * @param String navn på panelet der skal tændes/slukkes
     * @param boolean on/off 
     */
    public static void toggleAllsView(String viewName, boolean onoff)
    {
        ArrayList<ViewContainer> allViews = view.ViewSingleton.getSingletonObject().getAllViews();
        for (ViewContainer value : allViews)
        {
            if (value.getTheViewName().equals(viewName))
            {
                value.getTheView().setVisible(onoff);
                break;
            }
        }
    }
    
    /**
     * Find og returnér specifikt panel
     * @param String navn på panelet
     * @return JPanel
     */
    public static JPanel getAllViewsPanel(String specify)
    {
        ArrayList<ViewContainer> allViews = view.ViewSingleton.getSingletonObject().getAllViews();
        
        JPanel toReturn = null;
        for(ViewContainer value : allViews)
        {
            if (value.getTheViewName().equals(specify))
            {
                toReturn = value.getTheView();
            }
        }
        
        return toReturn;
    }
    
    /**
     * Retunér overskriften på specifikt panel
     * @param String navn på panelet
     * @return String
     */
    public static String getPageTitle(String specify)
    {
        ArrayList<ViewContainer> allViews = view.ViewSingleton.getSingletonObject().getAllViews();
        
        String toReturn = null;
        for(ViewContainer value : allViews)
        {
            if (value.getTheViewName().equals(specify))
            {
                toReturn = value.getPageTitle();
            }
        }
        
        return toReturn;
    }
    
    /**
     * Alle paneler registreres med x og y koordinater. Kald denne metode for at positionere panelet til standard værdier
     * @param String navn på panelet 
     */
    public static void assignStandardPositionToPanel(String specify)
    {
        ArrayList<ViewContainer> mostViews = view.ViewSingleton.getSingletonObject().getAllViews();
        
        for(ViewContainer value : mostViews)
        {
            if (value.getTheViewName().equals(specify))
            {
                value.getTheView().setBounds(value.getPosX(), value.getPosY(), value.getTheView().getWidth(), value.getTheView().getHeight());
            }
        }
    }
    
    /**
     * Alle paneler registreres med x og y koordinater.
     * Brug denne metode til at overskrive disse standard værdier og set nye bounds.
     * Panelets oprindelige standard værdier vil stadig være at finde i panelets ViewContainer
     * @param String navn på panelet
     * @param int x
     * @param int y 
     */
    public static void assignSpecifiedPositionToPanel(String specify, int x, int y)
    {
        ArrayList<ViewContainer> mostViews = view.ViewSingleton.getSingletonObject().getAllViews();
        
        for(ViewContainer value : mostViews)
        {
            if (value.getTheViewName().equals(specify))
            {
                value.getTheView().setBounds(x, y, value.getTheView().getWidth(), value.getTheView().getHeight());
            }
        }
    }
    
    /**
     * Ændr titlen der vises i programmets design
     * @param String ny overskrift
     */
    public static void setPageTitle(String txt)
    {
        JLabel lbl = (JLabel) getAllViewsPanel("pageTitle").getComponent(0);
        lbl.setText(txt);
    }
    
    /**
     * Fjern border på specificeret panel
     * @param String navn på panelet 
     */
    public static void turnOffBorder(String specify)
    {
        ArrayList<ViewContainer> allViews = view.ViewSingleton.getSingletonObject().getAllViews();
        
        for(ViewContainer value : allViews)
        {
            if (value.getTheViewName().equals(specify))
            {
                value.getTheView().setBorder(null);
            }
        }
    }
}
